<template>
	<view class="container">
		<!-- <recomm-list2 :guess="guess"></recomm-list2> -->

		<!-- 积分兑换 （内容模板1不可用）0/2-->
		<!-- <integral-mall :Productid="integralId" :product="productList" :cartId="4" @eventClick='nvto'></integral-mall> -->

		<!-- 优惠劵模板 -->
		<!-- <choice-color @onChoice="Choice" :couponList="''"></choice-color> -->
		<!-- 区域模板 -->
		<!-- <product-list :title="'标题'" :Productid="2" :product="productList" :cartId="3"></product-list> -->
		<!-- <text @click="productAll">测试测试</text>ref="productAll" -->
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';
	let goodsList = [];
	export default {
		components: {

		},
		data() {
			return {

			};
		},
		onShareAppMessage() {
			// const promise = new Promise(resolve => {
			// 	setTimeout(() => {
			// 		resolve({
			// 			title: '自定义转发标题'
			// 		})
			// 	}, 2000)
			// })
			// return {
			// 	title: '自定义转发标题',
			// 	path: '/page/index/index?scene=3%3A821%3A1%3AMall',
			// 	promise
			// }
			let corpId = uni.getStorageSync('corpid');
			let stockId = uni.getStorageSync('stockId');
			let hostid = uni.getStorageSync('hostid');
			let param = encodeURIComponent(corpId + ":" + stockId + ":" + hostid + ":Mall");
			return {
				title: '微商城',
				path: '/pages/index/index?scene=' + param
			}
		},
		onTabItemTap(item) {
			
		},
		onShareTimeline(){
			
		},
		onLoad(options) {
			console.log('ss', options)
			uni.showLoading({
				title: '加载中...'
			})
			let Appid = uni.getAccountInfoSync();
			this.$appid = Appid.miniProgram.appId; //小程序的appid
			//小程序跳转过来的记录
			let appdata = uni.getStorageSync('extraData');
			let scan = '';
			if (appdata !== '') {
				scan = appdata.scan; //小程序跳转过来带有的参数(类似扫码进来时获取到的数据)
			}
			if (JSON.stringify(options) === '{}') {
				let Oldoptions = uni.getStorageSync('options')
				if (Oldoptions != '') {
					options = Oldoptions;
					if (scan !== '') {
						options = scan;
					}
					wx.setStorageSync('options', options);
					this.init(options);
				} else {
					//当前进来没有入口参数
					this.$hostid = 3;
					this.$stockId = 15;
					this.$corpid = 12;
					options.corpid = this.$corpid;
					options.FItemID = this.$stockId;
					options.hostid = this.$hostid;
					uni.setStorageSync('hostid', this.$hostid);
					uni.setStorageSync('stockId', this.$stockId);
					uni.setStorageSync('corpid', this.$corpid);
					options.PagePath = 'Member'
					uni.setStorageSync('options', options)
					wx.setStorageSync('options', JSON.stringify(options));
					this.init(JSON.stringify(options));
					// this.oldLogin();
				}
			} else {
				if (scan !== '') {
					options = scan;
				}
				wx.setStorageSync('options', JSON.stringify(options));
				this.init(JSON.stringify(options));
			}
		},
		onShow() {
			//通过vuex来控制按钮中的title值
			// let title = this.integralId==3?'立即兑':'立即兑换';
			// this.shoppCart(title);
		},
		watch: {

		},
		methods: {
			...mapMutations(['shoppCart']),
			//初始化
			init(options) {
				// uni.hideLoading();
				this.$Url = uni.getStorageSync('URL') || this.$util.getUrls();
				options = JSON.parse(options);
				if (options.scene) {
					let scene = decodeURIComponent(options.scene); // 使用decodeURIComponent解析  获取当前二维码的网址
					let arr = scene.split(':');
					this.$hostid = arr[2]; //服务器id
					this.$stockId = arr[1]; //门店id
				} else {
					this.$corpid = options.corpid; //公司id
					this.$hostid = options.hostid; //服务器id
					this.$stockId = options.FItemID; //门店id
				}
				let Oldhostid = uni.getStorageSync('hostid');
				if (Oldhostid != this.$hostid) {
					if (Oldhostid === '') {
						uni.setStorageSync('Oldhostid', this.$hostid);
					} else {
						uni.setStorageSync('Oldhostid', Oldhostid);
					}
					/**
					 * 通过请求头去转换服务器地址和数据库，获取新的服务器地址头，方便以后的多个服务器
					 * 判断当前的hostid是否和之前的不一样，是就删除之前记录的服务器地址头
					 * 程序一进来都必须先用接口获取新的服务器地址头去请求数据
					 */
					// uni.removeStorageSync('URL');
					uni.removeStorageSync('Token');
					uni.setStorageSync('LoginID', 0);
				}
				uni.setStorageSync('hostid', this.$hostid);
				uni.setStorageSync('stockId', this.$stockId);
				uni.setStorageSync('corpid', this.$corpid);
				
				uni.login({
					success: (res) => {
						let code = res.code;
						this.$util.Login(code).then(res => {
							this.getHostUrl();
						}).catch(error => {
							// console.log('errr',error);
							this.getHostUrl();
						});
					}
				})
			},
			//获取上一次登录的信息
			oldLogin() {
				let token = uni.getStorageSync('Token'); //Token值
						mSystemAppID = uni.getStorageSync('mSystemAppID')
				uni.request({
					url: this.$Url + 'sale/CheckTokenLogin', //判断当前Token是否有效
					data: '',
					method: 'POST',
					header: {
						'token': token,
						'cookie': uni.getStorageSync('Cookies'),
						'mSystemAppID':mSystemAppID
					},
					success: (res) => {
						// if (res.data.msg == 'success') {
						// this.$corpid = uni.getStorageSync('corpid'); //公司id
						// this.$hostid = uni.getStorageSync('hostid'); //服务器id
						// this.$stockId = uni.getStorageSync('stockId'); //门店id
						// } else {
						// this.$api.msg('token值失效，请重新登录')
						// this.HostUrl().then(UrlRes => {
						uni.login({
							success: (loginRes) => {
								this.$util.util('WeChatShop/GetLastScene', {
									code: loginRes.code,
									appid: this.$appid,
								}, 'GET').then(res => {
									var data = res.scene;
									//如果数据库找不到之前登陆的值，就会返回一个<1>
									if (data != 1) {
										wx.setStorageSync('options', data);
										this.init(data);
									} else {
										/**
										 * corpid:3,hostid:1,FItemID:821 =>正(默认)
										 * corpid:1,hostid:2,FItemID:1 =>测(默认)
										 * corpid:12,hostid:3,FItemID:15 =>博亚城(默认)
										 */
										this.init(JSON.stringify({
											corpid: 1,
											hostid: 2,
											FItemID: 1
										}));
									}
								}).catch(error => {
									console.log(error);
									uni.showModal({
										title: '提示',
										content: '请重新扫码进入!!!',
									})
								})
							}
						})
						// })
						// }
					},
					fail: (error) => {
						console.log(error, this.$Url);
					}
				})
			},
			HostUrl() {
				/**
				 * 默认个hostid 1=>正 2=>测 3=>博亚城
				 */
				let hostid = this.$hostid || 2;
				return new Promise((resolve, reject) => {
					uni.request({
						url: this.$Url + 'XcxShopHome/GetHostUrl',
						data: {
							hostid: hostid
						},
						method: 'GET',
						header: {
							'content-type': 'application/json'
						}, // 默认值
						success: res => {
							// uni.setStorageSync('URL', res.data);
							resolve(res.data);
						}
					})
				});
			},
			//获取服务器地址头
			getHostUrl() {
				this.HostUrl().then(res => {
					this.$util.util('XcxShopHome/GetCorpID', {
						stockid: this.$stockId
					}, 'POST').then(res => {
						this.$corpid = res;
						uni.setStorageSync('corpid', this.$corpid)
						// this.loadData();
						this.onShunt('Mall');
					})
				})
			},
			//分流，看入口的参数是去哪里的
			onShunt(urls) {
				let url = '';
				if (urls === 'Mall') { //商城
					uni.reLaunch({
						url: `/pages/mallIndex/mallIndex`,
					})
				} else if (urls === 'Member') { //会员卡
					uni.redirectTo({
						url: `/pages/user/MemberUser`,
					})
				}
			},
		},
	}
</script>

<style lang="scss">
	page {
		background: #f5f5f5;
	}
</style>
